{
module HaCWB.Parser.Lexer (lex_tok) where

import Control.Monad.State (StateT, get)
import HaCWB.Parser.ParserM (ParserM (..), mkT, Token(..), St, start_code,
                StartCode, ActionAlex, set_start_code,
                show_pos, position, input,
                AlexInput, alexGetChar, alexInputPrevChar)
import Control.Monad.Trans


}

$act  = [a-z]
$proc = [A-Za-z0-9]

tokens :-
  $white+ ;
  "--".*  ;
  "#".*   ;
  \<    { mkT SYNOutOp } -- CCS Anotado / PiCalculus
  \>    { mkT SYNOutCl } -- CCS Anotado / PiCalculus
  \;    { mkT SYNFim }
  "0"   { mkT SYNZero }
  \|    { mkT SYNParallel }
  \{    { mkT SYNOGib }
  \}    { mkT SYNCGib }
  \(    { mkT SYNOBra }
  \)    { mkT SYNCBra }
  \,    { mkT SYNComma }
  \~    { mkT SYNNot }
  \+    { mkT SYNChoice }
  \.    { mkT SYNDot }
  \=    { mkT SYNEQ } -- \_ -> SIMB
  [nN][eE][wW]  { mkT SYNNew } 
  [A-Za-z]$proc+ { \(a, s) -> lift $ return(SYNProcName s, a) }	
  $act+ { \(a, s) -> lift $ return(SYNAct s, a) }
{

get_tok :: AlexInput -> StateT St (Either String) (Token, AlexInput)
get_tok = \i ->
   do st <- get
      case alexScan i (start_code st) of
          AlexEOF -> return (TEOF, i)
          AlexError _ -> fail $ "Unknown character at " ++ show_pos (position i)
          --AlexError _ -> fail $ "Lexical error at " ++ show_pos (position i)
          AlexSkip i' _ -> get_tok i'
          AlexToken i' l a -> a (i', take l (input i))

begin :: StartCode -> ActionAlex
begin sc (i, _) = do set_start_code sc
                     get_tok i

lex_tok :: (Token -> ParserM a) -> ParserM a
lex_tok cont = ParserM $ \i ->
   do (tok, iz) <- get_tok i
      case cont tok of
          ParserM x -> x iz

}
